package com.pickgliss.ui.controls.alert
{
	import com.pickgliss.events.FrameEvent;
	import com.pickgliss.events.InteractiveEvent;
	import com.pickgliss.toplevel.StageReferance;
	import com.pickgliss.ui.AlertManager;
	import com.pickgliss.ui.ComponentFactory;
	import com.pickgliss.ui.ComponentSetting;
	import com.pickgliss.ui.controls.BaseButton;
	import com.pickgliss.ui.controls.Frame;
	import com.pickgliss.ui.controls.TextButton;
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.ui.vo.AlertInfo;
	import com.pickgliss.utils.ObjectUtils;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	public class BaseAlerFrame extends Frame
	{
		public static const P_buttonToBottom:String = "buttonToBottom";
		public static const P_cancelButton:String = "submitButton";
		public static const P_info:String = "info";
		public static const P_submitButton:String = "submitButton";

		public function BaseAlerFrame()
		{
		}
		
		protected var _buttonToBottom:int;
		protected var _cancelButton:BaseButton;
		protected var _cancelButtonStyle:String;
		protected var _info:AlertInfo;
		protected var _sound:*;
		protected var _submitButton:BaseButton;
		protected var _submitButtonStyle:String;
		
		/**
		 * 
		 * @param value 按钮距离底部的距离（不包括按钮本身的高度）
		 * 
		 */		
		public function set buttonToBottom(value:int):void
		{
			if(_buttonToBottom == value)return ;
			_buttonToBottom = value;
			onPropertiesChanged(P_buttonToBottom);
		}
		
		public function set cancelButtonEnable(value:Boolean):void
		{
			_cancelButton.enable = value;
		}
		/**
		 * 
		 * @param stylename 取消按钮的样式(右边按钮)。通过ComponentFactory创建
		 * 
		 */		
		public function set cancelButtonStyle(stylename:String):void
		{
			if(_cancelButtonStyle == stylename)return;
			_cancelButtonStyle = stylename;
			_cancelButton = ComponentFactory.Instance.creat(_cancelButtonStyle);
			onPropertiesChanged(P_cancelButton);
		}

		override public function dispose():void
		{
			var focusDisplay:DisplayObject = StageReferance.stage.focus as DisplayObject;
			if(focusDisplay && contains(focusDisplay))StageReferance.stage.focus = null;
			if(_submitButton)
			{
				_submitButton.removeEventListener(MouseEvent.CLICK,__onSubmitClick);
				ObjectUtils.disposeObject(_submitButton);
				_submitButton = null;
			}
			
			if(_cancelButton)
			{
				_cancelButton.removeEventListener(MouseEvent.CLICK,__onCancelClick);
				ObjectUtils.disposeObject(_cancelButton);
				_cancelButton = null;
			}
			removeEventListener(KeyboardEvent.KEY_DOWN,__onKeyDown);
			_info = null;
			super.dispose();
		}
		
		public function get info():AlertInfo
		{
			return _info;
		}
		/**
		 * 
		 * @param value 提示框的信息 
		 * 具体见AlertInfo
		 * 
		 */		
		public function set info(value:AlertInfo):void
		{
			if(_info == value)return;
			if(_info)
			{
				_info.removeEventListener(InteractiveEvent.STATE_CHANGED,__onInfoChanged);
			}
			_info = value;
			_info.addEventListener(InteractiveEvent.STATE_CHANGED,__onInfoChanged);
			onPropertiesChanged(P_info);
		}
		
		public function set submitButtonEnable(value:Boolean):void
		{
			_submitButton.enable = value;
		}
		/**
		 * 
		 * @param stylename 确定按钮的样式（左边的按钮）
		 * 
		 */		
		public function set submitButtonStyle(stylename:String):void
		{
			if(_submitButtonStyle == stylename)return;
			_submitButtonStyle = stylename;
			_submitButton = ComponentFactory.Instance.creat(_submitButtonStyle);
			onPropertiesChanged(P_submitButton);
		}
		
		protected function __onCancelClick(event:MouseEvent):void
		{
			if(_sound != null)ComponentSetting.PLAY_SOUND_FUNC(_sound);
			onResponse(FrameEvent.CANCEL_CLICK);
		}
				
		override protected function __onCloseClick(event:MouseEvent):void{
			if(_sound != null)ComponentSetting.PLAY_SOUND_FUNC(_sound);
			super.__onCloseClick(event);
		}
		
		override protected function __onKeyDown(event:KeyboardEvent):void{
			if(event.keyCode == Keyboard.ENTER && enterEnable 
				|| event.keyCode == Keyboard.ESCAPE && escEnable)
			{
				if(_sound != null)ComponentSetting.PLAY_SOUND_FUNC(_sound);
			}
			super.__onKeyDown(event);
		}
		
		protected function __onSubmitClick(event:MouseEvent):void
		{
			if(_sound != null)ComponentSetting.PLAY_SOUND_FUNC(_sound);
			onResponse(FrameEvent.SUBMIT_CLICK);
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			if(_submitButton)addChild(_submitButton);
			if(_cancelButton)addChild(_cancelButton);
		}
		
		override protected function onProppertiesUpdate():void
		{
			if(_changedPropeties[P_info])
			{
				_sound = _info.sound;
				_escEnable = _info.escEnable;
				_enterEnable = info.enterEnable;
				_titleText = _info.title;
				_changedPropeties[Frame.P_titleText] = true;
				_moveEnable = _info.moveEnable;
				_changedPropeties[Frame.P_moveEnable] = true;
			}
			super.onProppertiesUpdate();
			if(_changedPropeties[P_info] || _changedPropeties[P_submitButton] || _changedPropeties[P_cancelButton])
			{
				if(_cancelButton && _info)
				{
					_cancelButton.visible = _info.showCancel;
					_cancelButton.enable = _info.cancelEnabled;
					if(_cancelButton is TextButton)TextButton(_cancelButton).text = _info.cancelLabel;
					if(_cancelButton.visible)_cancelButton.addEventListener(MouseEvent.CLICK,__onCancelClick);
				}
				if(_submitButton && _info)
				{
					_submitButton.visible = _info.showSubmit;
					_submitButton.enable = _info.submitEnabled;
					if(_submitButton is TextButton)TextButton(_submitButton).text = _info.submitLabel;
					if(_submitButton.visible)_submitButton.addEventListener(MouseEvent.CLICK,__onSubmitClick);
				}
			}
			if(_changedPropeties[P_info] || _changedPropeties[Component.P_height] || _changedPropeties[Component.P_width] || _changedPropeties[P_buttonToBottom])
			{
				updatePos();
			}
		}
		
		override protected function onResponse(type:int):void
		{
			if(_info && _info.autoDispose)dispose();
			super.onResponse(type);
		}
		
		protected function updatePos():void
		{
			if(_info == null) return;
			if(_info.bottomGap)_buttonToBottom=int(_info.bottomGap);
			if(_submitButton)_submitButton.y = _height - _submitButton.height -  _buttonToBottom;
			if(_cancelButton)_cancelButton.y = _height - _cancelButton.height - _buttonToBottom;
			if(_info.showCancel || _info.showSubmit)
			{
				if(_info.customPos){
					if(_submitButton){
						_submitButton.x = _info.customPos.x;
						_submitButton.y = _info.customPos.y;
						
						if(_cancelButton){
							_cancelButton.x = _info.customPos.x + _cancelButton.width + _info.buttonGape;
							_cancelButton.y = _info.customPos.y;
						}
					} else if(_cancelButton){
						_cancelButton.x = _info.customPos.x;
						_cancelButton.y = _info.customPos.y;
					}
				} else {
					if(_info.autoButtonGape)
					{
						if(_submitButton != null && _cancelButton != null)
						{
							_info.buttonGape = (_width - _submitButton.width -_cancelButton.width)/2;
						}
					}
					if(!_info.showCancel && _submitButton)
					{
						_submitButton.x = (_width - _submitButton.width)/2;
					}else if(!_info.showSubmit && _cancelButton)
					{
						_cancelButton.x =  (_width - _cancelButton.width)/2;
					}else
					{
						if(_cancelButton != null && _submitButton != null)
						{
							_submitButton.x = (_width - _submitButton.width - _cancelButton.width - _info.buttonGape)/2;
							_cancelButton.x = _submitButton.x+_submitButton.width + _info.buttonGape;
						}
					}
				}
			}
		}

		private function __onInfoChanged(event:InteractiveEvent):void
		{
			onPropertiesChanged(P_info);
		}
	}
}